package com.advert.presentation.bean;

import com.advert.exception.AuthenticationException;
import com.advert.presentation.tools.FacesHelper;
import com.advert.presentation.util.ActionOutcome;
import com.advert.security.AppSecurityManager;
import com.advert.security.Credentials;
import com.advert.presentation.util.localization.LabelsMap;
import com.advert.service.security.SecurityService;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import java.io.Serializable;

@Name("authBean")
@Scope(ScopeType.PAGE)
public class AuthBean implements Serializable {
    @In("SecurityManager")
    private AppSecurityManager securityManager;
    @In("facesHelper")
    private FacesHelper facesHelper;
    @In("labels")
    private LabelsMap labels;

    private Credentials credentials;
    private String activatedUserId;
    private String activationCode;


    // Accessors

    public Credentials getCredentials() {
        return credentials;
    }

    public String getActivatedUserId() {
        return activatedUserId;
    }

    public void setActivatedUserId(String activatedUserId) {
        this.activatedUserId = activatedUserId;
    }

    public String getActivationCode() {
        return activationCode;
    }

    public void setActivationCode(String activationCode) {
        this.activationCode = activationCode;
    }

    
    // Actions

    public String loginUser() {
        String retVal = ActionOutcome.FAILED.name();
        try {
            if (securityManager.login(credentials)) {
                retVal = ActionOutcome.SUCCESS.name();
            }
        } catch (AuthenticationException e) {
            switch (e.getReason()) {
                case NO_SUCH_LOGIN:
                    facesHelper.addError(labels.get("LOGIN_OR_PASSWORD_INCORRECT"), false);
                    break;
                case PASSWORD_INCORRECT:
                    facesHelper.addError(labels.get("LOGIN_OR_PASSWORD_INCORRECT"), false);
                    break;
                case USER_NOT_ACTIVE:
                    facesHelper.addError(labels.get("USER_NOT_ACTIVE"), false);
                    break;
            }
        }
        return retVal;
    }

    public void changePassword() {
        credentials.setLogin(securityManager.userLogin());
        if (!credentials.getNewPassword().equals(credentials.getPasswordConfirm())) {
            facesHelper.addError(labels.get("PASSWORD_CONFIRM_INVALID"), false);
        } else {
            try {
                securityManager.changePassword(credentials);
                facesHelper.addInfo(labels.get("PASSWORD_CHANGED_SUCCESSFULLY"), false);
            } catch (AuthenticationException e) {
                facesHelper.addError(labels.get("PASSWORD_INCORRECT"), false);
            }
        }     
    }

    public String logout() {
        securityManager.logout();
        return ActionOutcome.SUCCESS.name();
    }

    public String registerUser() {
        String retVal = ActionOutcome.FAILED.name();
        try {
            if (!credentials.getPassword().equals(credentials.getPasswordConfirm())) {
                facesHelper.addError(labels.get("PASSWORD_CONFIRM_INVALID"), false);
            } else if (securityManager.registerUser(credentials)) {
                retVal = ActionOutcome.SUCCESS.name();
                facesHelper.addInfo(labels.get("REGISTRATION_SUCCESS_MESSAGE"), true);
            }
        } catch (AuthenticationException e) {
            switch (e.getReason()) {
                case LOGIN_ALREADY_EXISTS:
                    facesHelper.addError(labels.get("LOGIN_ALREADY_EXISTS"), false);
                    break;
            }
        }
        return retVal;
    }

    public String activateUser() {
        String retVal = ActionOutcome.FAILED.name();
        try {
            if (securityManager.activateUser(activatedUserId, activationCode)) {
                retVal = ActionOutcome.SUCCESS.name();
                facesHelper.addInfo(labels.get("ACTIVATION_SUCCESS_MESSAGE"), true);
            }
        } catch (AuthenticationException e) {}

        return retVal;
    }

    public String recoverPassword() {
        String retVal = null;
        try {
            securityManager.recoverPassword(credentials);
            retVal = ActionOutcome.SUCCESS.name();
            facesHelper.addInfo(labels.get("PASS_RECOVERY_MESSAGE"), true);
        } catch (AuthenticationException e) {
            if (e.getReason() == AuthenticationException.Reason.NO_SUCH_LOGIN_OR_MAIL) {
                facesHelper.addError(labels.get("NO_SUCH_LOGIN_OR_MAIL"), false);
            }
        }
        return retVal;
    }


    @Create
    public void init() {
        credentials = new Credentials();
    }
}
